package com.hycreate.platform.modules.voice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Config;
import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.sdk.tts.tts.HciCloudTtsMain;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.common.utils.DateUtils;
import com.hycreate.platform.common.utils.FileUtils;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.modules.sys.utils.UserUtils;
import com.hycreate.platform.modules.voice.SfVoice;
import com.hycreate.platform.modules.voice.SfVoiceService;

/**
 * 语音播报Controller
 * @author 语音播报
 * @version 2017-02-22
 */
@Controller
@RequestMapping(value = "${adminPath}/voice/sfVoice")
public class SfVoiceController extends BaseController {

	@Autowired
	private SfVoiceService sfVoiceService;
	
	@ModelAttribute
	public SfVoice get(@RequestParam(required=false) String id) {
		SfVoice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfVoiceService.get(id);
		}
		if (entity == null){
			entity = new SfVoice();
		}
		return entity;
	}
	
	@RequiresPermissions("voice:sfVoice:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfVoice sfVoice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfVoice> page = sfVoiceService.findPage(new Page<SfVoice>(request, response), sfVoice); 
		model.addAttribute("page", page);
		return "modules/voice/sfVoiceList";
	}

	@RequiresPermissions("voice:sfVoice:view")
	@RequestMapping(value = "form")
	public String form(SfVoice sfVoice, Model model) {
		List<SfVoice> list = sfVoiceService.findList(new SfVoice());
		if(CollectionUtils.isNotEmpty(list)){
			sfVoice = list.get(0);
		}
		model.addAttribute("sfVoice", sfVoice);
		return "modules/voice/sfVoiceForm";
	}

	@RequiresPermissions("voice:sfVoice:edit")
	@RequestMapping(value = "save")
	public String save(SfVoice sfVoice, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (!beanValidator(model, sfVoice)){
			return form(sfVoice, model);
		}
		
		/**
		 * 新增、修改内容需要重新播报
		 */
		if(StringUtils.isNotEmpty(sfVoice.getId())){
			SfVoice sfVoiceOld = sfVoiceService.get(sfVoice.getId());
			if(StringUtils.isNotEmpty(sfVoice.getContext()) && !sfVoice.getContext().equals(sfVoiceOld.getContext())){
				sfVoice.setStatus("1");
				createVoice(sfVoice);
			}
		}else{
			sfVoice.setStatus("1");
			createVoice(sfVoice);
		}
		sfVoiceService.save(sfVoice);
		addMessage(redirectAttributes, "保存语音播报成功");
//		return "redirect:"+Global.getAdminPath()+"/voice/sfVoice/?repage";
		return "redirect:"+Global.getAdminPath()+"/voice/sfVoice/form";
	}

	/**
	 * 创建语音文件
	 * @param sfVoice
	 */
	private void createVoice(SfVoice sfVoice) {
		String sPath = Config.getConfig("project.base.path");

		String contentFilePath = sPath + "/sdk/tts/context.txt";
//		FileUtils.deleteFile(contentFilePath);
		FileUtils.writeToFile(contentFilePath, sfVoice.getContext(), false);
		
		/**
		 * pcm文件生成路径
		 */
		StringBuffer filePath = new StringBuffer();
		filePath.append(sPath);
//		filePath.append("D:/");
		filePath.append("/userfiles/").append(UserUtils.getUser().getId()).append("/voice/");
		filePath.append(DateUtils.getDate("yyyyMMddHHmmss")).append(".pcm");
		
		System.out.println("pcm文件生成路径：" + filePath);
		
		/**
		 * web访问路径
		 */
		StringBuffer webUrl = new StringBuffer();
		webUrl.append("|/").append(Global.getConfig("redis.keyPrefix"));
		webUrl.append("/userfiles/").append(UserUtils.getUser().getId()).append("/voice/");
		webUrl.append(DateUtils.getDate("yyyyMMddHHmmss")).append(".wav");
		System.out.println("web访问路径：" + webUrl);
		
		HciCloudTtsMain.execute(sPath, filePath.toString(), sfVoice.getContext());
		sfVoice.setVoiceUrl(webUrl.toString());
	}
	
	@RequiresPermissions("voice:sfVoice:edit")
	@RequestMapping(value = "delete")
	public String delete(SfVoice sfVoice, RedirectAttributes redirectAttributes) {
		sfVoiceService.delete(sfVoice);
		addMessage(redirectAttributes, "删除语音播报成功");
		return "redirect:"+Global.getAdminPath()+"/voice/sfVoice/?repage";
	}

}